//
//  main.cpp
//  L12FunctionPointer
//
//  Created by ai-nixs on 2017/10/29.
//  Copyright © 2017年 nixinsheng. All rights reserved.
//

#include <iostream>
#include <thread>
#include <unistd.h>//sleep
using namespace std;

class Object;

typedef void(Object::*SayHi)();//定义一种类型
typedef void(Object::*CallaterHandler)();
#define CH(fp) (CallaterHandler)(&fp)

void threadFunc(Object *target,CallaterHandler handler,int delay){
    sleep(delay);
    (target->*handler)();
}

void callater(Object *target,CallaterHandler handler,int delay){
    thread t(threadFunc,target,handler,delay);//定义一个thread,传入一个方法
    t.join();//等待线程的结束
}


class Object {
    
public:
    //C++函数指针：指向的函数必须是Object的成员或者是Object子类的成员
    //void (Object::*sayHi)();//Object::类的限定
    SayHi sayHi;
};

class Hello:public Object {//继承Object
    
public:
    Hello(){//默认构造
        //sayHi = (void(Object::*)())(&Hello::HelloSayHi);
        
        /*
        sayHi = (SayHi)(&Hello::HelloSayHi);//(SayHi)为目的要转成的类型
        (this->*sayHi)();
         */
        cout<<"[🔔延时3s继续执行！！！]"<<endl;
        //callater(this, (CallaterHandler)(&Hello::HelloSayHi), 3);
        callater(this, CH(Hello::HelloSayHi), 3);
    }
    
    void HelloSayHi(){
        printf("Hello CPP.\n");
    }
};


int main(int argc, const char * argv[]) {
    Hello *hello = new Hello();
    delete hello;
    
    return 0;
}
